/**
 * @title: FlowRuleMysqlConverter
 * @projectName mscloud
 * @Description TODO
 * @Author
 * @Date 2022/2/23 13:09
 */
package com.atguigu.springcloud.cloudsentinelgatewaydevelop.sentinel.convert.impl;

import com.alibaba.csp.sentinel.slots.block.flow.ClusterFlowConfig;
import com.alibaba.csp.sentinel.slots.block.flow.FlowRule;
import com.atguigu.springcloud.cloudsentinelgatewaydevelop.bean.dto.SentinelFlowRuleDTO;
import com.atguigu.springcloud.cloudsentinelgatewaydevelop.sentinel.convert.PullConverter;
import lombok.Data;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.stream.Collectors;
@Data
@Component
public class FlowRuleMysqlConverter implements PullConverter<List<SentinelFlowRuleDTO>,List<FlowRule>> {
    private long recommendRefreshMs=10000;
    @Override
    public List<FlowRule> convert(List<SentinelFlowRuleDTO> sources) {
         return sources.stream()
                .map(
                        item->{
                            FlowRule flowRule=new FlowRule();
                            //JSONObject jsonObject = JSON.parseObject(item);
                            //JSONObject clusterConfig = jsonObject.getJSONObject("clusterConfig");
                            ClusterFlowConfig clusterConfig = item.getClusterConfig();
                            if(clusterConfig!=null){
                                flowRule.setClusterConfig(clusterConfig);
                                //flowRule.setClusterConfig(clusterConfig.toJavaObject(ClusterFlowConfig.class));
                            }
                            flowRule.setClusterMode(item.getClusterMode());
                            flowRule.setControlBehavior(item.getControlBehavior());
                            flowRule.setCount(item.getCount());
                            flowRule.setGrade(item.getGrade());
                            flowRule.setMaxQueueingTimeMs(item.getMaxQueueingTimeMs());
                            flowRule.setRefResource(item.getRefResource());
                            flowRule.setStrategy(item.getStrategy());
                            flowRule.setWarmUpPeriodSec(item.getWarmUpPeriodSec());
                            flowRule.setLimitApp(item.getLimitApp());
                            flowRule.setResource(item.getResource());
                            return flowRule;
                        }
                )
                .collect(Collectors.toList());
    }
}
